package com.fs.erp.api.basedata.controller;

import com.fs.erp.api.basedata.bo.product.QueryProductSalePropBo;
import com.fs.erp.api.basedata.entity.ProductSale;
import com.fs.erp.api.basedata.entity.ProductSalepropGroup;
import com.fs.erp.api.basedata.service.ProductSalepropService;
import com.fs.erp.api.basedata.vo.UpdateProductSalepropVo;
import com.fs.erp.api.basedata.vo.product.QueryProductSalePropVo;
import com.fs.erp.common.utils.CollectionUtil;
import com.fs.erp.mybatis.resp.PageResult;
import com.fs.erp.mybatis.utils.PageResultUtil;
import com.fs.erp.web.controller.DefaultBaseController;
import com.fs.erp.web.resp.InvokeResult;
import com.fs.erp.web.resp.InvokeResultBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/basedata/product/saleprop/group")
@RestController
public class ProductSalePropController extends DefaultBaseController {
    @Autowired
    private ProductSalepropService productSalepropService;

    @GetMapping("/query")
    public InvokeResult<PageResult<QueryProductSalePropBo>> query(@Valid QueryProductSalePropVo vo){
        PageResult<ProductSalepropGroup> salepropGroupPageResult=productSalepropService.query(getPageIndex(vo),getPageSize(vo),vo);
        List<ProductSalepropGroup> salepropGroups=salepropGroupPageResult.getDatas();
        List<QueryProductSalePropBo> bos=null;

        if (!CollectionUtil.isEmpty(salepropGroups)){
            bos=salepropGroups.stream().map(QueryProductSalePropBo::new).collect(Collectors.toList());
        }
        return InvokeResultBuilder.success(PageResultUtil.rebuild(salepropGroupPageResult,bos));
    }

    /**
     * 修改
     */
    @PutMapping
    public InvokeResult<Void> update(@Valid UpdateProductSalepropVo vo){

        productSalepropService.updateSaleprop(vo);
        productSalepropService.cleanCacheByKey(vo.getId());
        return InvokeResultBuilder.success();
    }
    @GetMapping
    public InvokeResult<QueryProductSalePropBo> getInfo(@RequestParam("id")String id){
        QueryProductSalePropBo salePropBo=productSalepropService.selectById(id);
        return InvokeResultBuilder.success(salePropBo);
    }
}
